(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

(*-----------------------------------------------------------------------*)

open Pycaml

module Camomile = CamomileLibraryDefault.Camomile
module CharEncoding = Camomile.CharEncoding
module UChar = Camomile.UChar
module UText = Camomile.UText

(*-----------------------------------------------------------------------*)

module UniChar =
struct
  type t = UChar.t
  let eq = UChar.eq
  let compare = UChar.compare
  let of_int = UChar.of_int
  let to_int = UChar.int_of
  let int_of = UChar.int_of
(*
  let sexp_of_t u = Conv.sexp_of_int (to_int u)
  let t_of_sexp s = of_int (Conv.int_of_sexp s)
*)
end

module UniString =
struct
  module type T =
  sig
    include Camomile.UnicodeString.Type
    val append : t -> t -> t
  end

  module type S =
  sig
    type t (* with sexp *)
    val of_string : ?encoding:string -> string -> t
    val to_string : ?encoding:string -> t -> string
    val string_of : ?encoding:string -> t -> string
    val of_ints : int array -> t
    val to_ints : t -> int array
    val ints_of : t -> int array
    val of_int_list : int list -> t
    val to_int_list : t -> int list
    val int_list_of : t -> int list
    val of_uniChars : UniChar.t array -> t
    val to_uniChars : t -> UniChar.t array
    val uniChars_of : t -> UniChar.t array
    val of_uniChar_list : UniChar.t list -> t
    val to_uniChar_list : t -> UniChar.t list
    val uniChar_list_of : t -> UniChar.t list
    val of_glyph_name_string : string -> t
    val of_glyph_name : t -> t
    val empty  : t
    val length : t -> int
    val append : t -> t -> t
    val get    : t -> int -> UniChar.t
    val pythonize : t -> Pycaml.pyobject
    val unpythonize : ?encoding:string -> Pycaml.pyobject -> t
  end

  module Make (Text : T) =
  struct
    type t = Text.t
    module Codec = CharEncoding.Make(Text)

    let of_string ?encoding =
      match encoding with
        | None -> Codec.decode CharEncoding.utf8
        | Some e -> Codec.decode (CharEncoding.of_name e)

    let to_string ?encoding =
      match encoding with
        | None -> Codec.encode CharEncoding.utf8
        | Some e -> Codec.encode (CharEncoding.of_name e)

    let string_of = to_string

(*
    let sexp_of_t u = Conv.sexp_of_string (to_string u)
    let t_of_sexp s = of_string (Conv.string_of_sexp s)
*)

    let of_ints a = Text.init (Array.length a) (fun i -> UniChar.of_int a.(i))
    let to_ints u = Array.init (Text.length u) (fun i -> UniChar.int_of (Text.get u i))
    let ints_of   = to_ints


    let of_int_list p = of_ints (Array.of_list p)
    let to_int_list u = Array.to_list (to_ints u)
    let int_list_of   = to_int_list

    let of_uniChars a = Text.init (Array.length a) (fun i -> a.(i))
    let to_uniChars u = Array.init (Text.length u) (fun i -> Text.get u i)
    let uniChars_of   = to_uniChars

    let of_uniChar_list p = of_uniChars (Array.of_list p)
    let to_uniChar_list u = Array.to_list (to_uniChars u)
    let uniChar_list_of   = to_uniChar_list

    let empty = of_ints [| |]

    let length = Text.length
    let append = Text.append
    let get    = Text.get

    let pythonize u =
      let text_length = Text.length u in
        pyunicode_fromunicode
          (fun i -> UniChar.int_of (Text.get u i))
          text_length

    let unpythonize ?encoding =
      unpythonizing_function
        [| EitherStringType |]
        begin
          fun py_args ->
            let py_u = py_args.(0) in
              match pytype py_u with
                | BytesType ->
                    of_string ?encoding (unpythonize_string py_u)
                | _ ->
                    let a = pyunicode_asunicode py_u in
                      Text.init (Array.length a) (fun i -> UniChar.of_int a.(i))
        end

    let of_glyph_name_string name = (* FIX: Write an OCaml/C version rather than call the Python/C version. *)
      let glyph_name_module = Python_tools.py_module "glyph_name" in
      let py_args = Pycaml.pytuple_fromsingle (Pycaml.pybytes_fromstring name) in
      let py_uni = Python_tools.py_module_call glyph_name_module "glyph_name_to_unicode" py_args in
        if py_uni = Pycaml.pynull () then
          empty
        else
          unpythonize py_uni

    let of_glyph_name u_name =
      of_glyph_name_string (to_string u_name)

  end
end

module UniText = UniString.Make(UText)

(*-----------------------------------------------------------------------*)
